#!/bin/bash
set -e
VERSION="5.2.6"
MIRROR="br.php.net"
MAINPHP="br2.php.net"
#MAINPHP="us.php.net"
MD5="1720f95f26c506338f0dba3a51906bbd"
PORT=50080

DEBUG="xdebug"
#DEBUG="zenddebug"
PATHINSTALL="$HOME/usr/php5"
PATHTMP="`pwd`"

PHPOPTIONS=" \
	--with-mysql \
	--with-mysql-sock \
	--with-mysqli \
	--with-pdo-mysql \
	--with-pgsql \
	--with-pdo-pgsql \
	--enable-inline-optimization \
	--disable-debug \
	--enable-bcmath \
	--enable-calendar \
	--enable-ctype \
	--enable-dbase \
	--enable-discard-path \
	--enable-exif \
	--enable-force-cgi-redirect \
	--enable-ftp \
	--with-ttf \
	--enable-magic-quotes \
	--enable-shmop \
	--enable-sigchild \
	--enable-sysvsem \
	--enable-sysvshm \
	--enable-wddx \
	--enable-dba
	--with-zlib=yes \
	--with-openssl \
	--with-xsl \
	--with-ldap \
	--with-ncurses \
	--with-gd \
	--with-jpeg-dir \
	--with-png-dir \
	--with-zlib-dir \
	--with-xpm-dir \
	--with-ttf \
	--with-freetype-dir \
	--enable-gd-native-ttf \
	--with-imap \
	--with-imap-ssl \
	--with-gettext \
	--with-mcrypt \
	--with-mhash \
	--enable-sockets \
	--enable-mbstring=all \
	--with-curl \
	--enable-mbregex \
	--enable-zend-multibyte \
	--enable-exif \
	--with-bz2 \
	--with-sqlite=shared \
	--enable-soap \
	--enable-pcntl \
	--with-mime-magic \
	--with-iconv \
	--with-ming=shared \
	--with-kerberos \
	--enable-fastcgi \
	--enable-cli \
"

XDEBUG_VERSION="2.0.3"

##############################################
##############################################
tar="php-$VERSION.tar.gz"
url="http://$MAINPHP/get/$tar/from/$MIRROR/mirror"

tar_xdebug="xdebug-$XDEBUG_VERSION.tgz"
url_xdebug="http://xdebug.org/link.php?url=xdebug"`echo "$XDEBUG_VERSION" | sed -e "s/\.//g"`

APACHE_PATHMODULES="/usr/lib/apache2/modules/"
PATHSCRIPT=`dirname $0`
PATHSCRIPT=`pwd`/$PATHSCRIPT
#PATHSCRIPT=`realpath $PATHSCRIPT`
PREFIXNAME=`basename $0 .sh`

source $PATHSCRIPT/util

OS=`getOSNameRev`
OSFileName=`echo "$OS" | tr "[A-Z] ." "[a-z]__"`
echo "$OS - $OSFileName"
distroinstall="distro.install.$OSFileName"
distrobuild="distro.build.$OSFileName"

makepkg=false
setup=true
build=true

if [ "$1" == "--clean" ] ; then
	echo "Cleanning..."
	rm -f $PATHSCRIPT/*~

	rm -rf $PATHINSTALL

	rm -rf $PATHTMP/php-$VERSION
	rm -f $PATHTMP/$tar
	rm -f $PATHTMP/$tar.md5

	rm -rf $PATHTMP/xdebug-$XDEBUG_VERSION
	rm -f $PATHTMP/xdebug-$XDEBUG_VERSION.tgz 

	rm -f $PATHSCRIPT/php-${VERSION}-${DEBUG}.run

	exit
fi

if [ "$1" == "--makepkg" ] ; then
	echo "Packaging..."
	PATHINSTALL=$PATHTMP/install/php5
	makepkg=true
	build=true
	setup=false
fi

if [ "$1" == "--setup" ] ; then
	echo "Setting up..."
	#PATHINSTALL=`realpath -s $PATHSCRIPT/../.`
	PATHINSTALL=$PATHSCRIPT/../.
	makepkg=false
	setup=true
	build=false
fi

if $build ; then
	if [ -d "$PATHINSTALL" ] ; then
		echo "The '$PATHINSTALL' already exist. It is recommended to install in empty path."
		exit
	fi

	# Verify if there is a zend archive to install
	if [ "$DEBUG" == "zenddebug" ] ; then
		tar_zenddebug=`ls -t ZendDebugger-*-glibc23-*.tar.gz | sed -ne '1p'`
		if [ ! -f "$PATHSCRIPT/$tar_zenddebug" ] ; then
			echo ""
			echo "Please, download Zend Studio Debugger (ZendDebugger-*-glibc23-*.tar.gz) from this URL" 
			echo "http://www.zend.com/en/products/studio/downloads and put it in '$PATHSCRIPT' directory"
			echo ""
			echo "Sorry, I couldn't do it automatically because Zend License approach"
			exit
		fi
		dir_zenddebug=`echo "$tar_zenddebug" | sed -e "s/\(.*\).tar.gz/\1/"`
	fi

	cd $PATHTMP

	# Distro Packages for Build
	if [ -f $PATHSCRIPT/$distrobuild ] ; then
		echo "Loading $PATHSCRIPT/$distrobuild"
		source $PATHSCRIPT/$distrobuild
	else
		echo "No tweaks distro file"
	fi

	# Unpack source
	if [ ! -f $PATHTMP/php-$VERSION.tar.gz ] ; then
		wget $url
	fi
	if [ ! -d $PATHTMP/php-$VERSION ] ; then
		echo "$MD5  $tar" > $tar.md5
		md5sum -c $tar.md5
		gunzip -c $tar | tar -xvf -
	fi
	cd php-$VERSION

	# Apply pre-install patchs
	for p in `ls $PATHSCRIPT/$PREFIXNAME-pre*.patch` ; do
		patch -b -N -p0 < $p || true
	done

	# Configure
	./configure \
		--prefix=$PATHINSTALL \
		--with-apxs2=`which apxs2` \
		$PHPOPTIONS

	# Compile
	make

	# Install
	mkdir -p $PATHINSTALL/lib
	cp $PATHTMP/php-$VERSION/libs/libphp5.so $PATHINSTALL/lib/.
	make install-cli
	make install-modules
	make install-build
	make install-headers
	make install-programs

	PATH=$PATHINSTALL/bin:$PATH
	LD_LIBRARY_PATH=$PATHINSTALL/lib:$LD_LIBRARY_PATH
	make install-pear

	# Apply post-install patchs
	cd $PATHINSTALL
	for p in `ls $PATHSCRIPT/$PREFIXNAME-post*.patch` ; do
		patch -b -N -p0 < $p || true
	done

	mkdir -p $PATHINSTALL/etc
	mkdir -p $PATHINSTALL/var/run
	mkdir -p $PATHINSTALL/var/lock

	cp $PATHSCRIPT/$PREFIXNAME.sh $PATHINSTALL/bin/$PREFIXNAME.sh
	cp $PATHSCRIPT/php-$VERSION/php.ini-recommended $PATHINSTALL/lib/php.ini
	cp $PATHSCRIPT/$PREFIXNAME-apache.conf $PATHINSTALL/etc/apache.conf
	cp $PATHSCRIPT/$PREFIXNAME-start-apache.sh $PATHINSTALL/bin/start-apache.sh
	cp $PATHSCRIPT/$PREFIXNAME-env.sh $PATHINSTALL/bin/env.sh

	chmod a+x $PATHINSTALL/bin/start-apache.sh

	if [ "$DEBUG" == "xdebug" ] ; then
		echo "Installing xdebug"
		cd $PATHTMP

		if [ ! -f $PATHTMP/$tar_xdebug ] ; then
			wget -O $tar_xdebug $url_xdebug
		fi
		if [ ! -d $PATHTMP/xdebug-$XDEBUG_VERSION ] ; then
			gunzip -c $tar_xdebug | tar -xvf -
		fi

		export PATH=$PATHINSTALL/bin:$PATH
		cd $PATHTMP/xdebug-$XDEBUG_VERSION
		phpize
		./configure --enable-xdebug
		make
		cp modules/xdebug.so $PATHINSTALL/lib/.
		echo "" >> $PATHINSTALL/lib/php.ini
		echo "zend_extension=\"$PATHINSTALL/lib/xdebug.so\"" >> $PATHINSTALL/lib/php.ini
		echo "" >> $PATHINSTALL/lib/php.ini

		cd $PATHTMP
	fi

	if [ "$DEBUG" == "zenddebug" ] ; then
		echo "Installing zenddebug"
		cd $PATHTMP
		gunzip -c $PATHSCRIPT/$tar_zenddebug | tar -xvf -
		cp $PATHTMP/$dir_zenddebug/5_2_x_comp/ZendDebugger.so $PATHINSTALL/lib/.
		echo "" >> $PATHINSTALL/lib/php.ini
		echo "zend_extension=\"$PATHINSTALL/lib/ZendDebugger.so\"" >> $PATHINSTALL/lib/php.ini
		echo "zend_debugger.allow_hosts=0.0.0.0/0" >> $PATHINSTALL/lib/php.ini
		echo "zend_debugger.expose_remotely=always" >> $PATHINSTALL/lib/php.ini
		echo "" >> $PATHINSTALL/lib/php.ini
		cp $PATHTMP/$dir_zenddebug/dummy.php $PATHINSTALL/lib/.
	fi
fi

if $setup ; then
	if [ -f $PATHINSTALL/etc/os ] ; then
		OSFile=`cat $PATHINSTALL/etc/os`
		if [ "$OSFile" != "$OS" ] ; then
			echo "This bundle was compiled to $OSFile not to $OS"
			exit
		fi
	fi

	# Distro Packages for Install
	if [ -f $PATHSCRIPT/$distroinstall ] ; then
		echo "Loading $PATHSCRIPT/$distroinstall"
		source $PATHSCRIPT/$distroinstall
	else
		echo "No tweaks distro file"
	fi

	perl -pi -e "s|__PHP5_PATHINSTALL__|$PATHINSTALL|g" $PATHINSTALL/lib/php/PEAR/Config.php
	perl -pi -e "s|__PHP5_PATHINSTALL__|$PATHINSTALL|g" $PATHINSTALL/etc/apache.conf
	perl -pi -e "s|__PHP5_PORT__|$PORT|g" $PATHINSTALL/etc/apache.conf
	perl -pi -e "s|__APACHE_PATHMODULES__|$APACHE_PATHMODULES|g" $PATHINSTALL/etc/apache.conf
	perl -pi -e "s|__PHP5_PATHINSTALL__|$PATHINSTALL|g" $PATHINSTALL/bin/start-apache.sh
	perl -pi -e "s|__PHP5_PATHINSTALL__|$PATHINSTALL|g" $PATHINSTALL/bin/env.sh

	echo "--- SUCCESS ---"
	echo "Use: $PATHINSTALL/bin/start-apache.sh"
fi

if $makepkg ; then
	echo "$OS" >  $PATHINSTALL/etc/os
	cp $PATHSCRIPT/util $PATHINSTALL/bin/util
	if [ -f  $PATHSCRIPT/$distroinstall ] ; then
		cp $PATHSCRIPT/$distroinstall $PATHINSTALL/bin/.
	fi

	#archive=`realpath -s $PATHINSTALL/../.`
	archive=$PATHINSTALL/../.
	makeself --current $archive $PATHSCRIPT/php-${VERSION}-${DEBUG}-${OSFileName}.run "php-${VERSION} ${DEBUG}" ./php5/bin/php5install.sh --setup
fi
